package com.spsoft.vatoutinv.jobHandler;

import cn.hutool.core.collection.CollectionUtil;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.model.Result;
import com.spsoft.core.model.bo.TokenUser;
import com.spsoft.vatoutinv.entity.vmsfullbswjmanager.VmsFullBswjManager;
import com.spsoft.vatoutinv.model.dto.vmsenterpriseinforisk.VmsEnterpriseInfoRiskDto;
import com.spsoft.vatoutinv.service.vmsenterpriseinforisk.VmsEnterpriseInfoRiskService;
import com.spsoft.vatoutinv.service.vmsfullbswjmanager.VmsFullBswjManagerService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 企业风险信息管理定时任务
 *
 * @author : chenxudong
 * @date : 2024/3/6 9:16
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class VmsEnterpriseInfoRiskJob {

    public static final String LOG_MSG = "(获取纳税人风险信息定时任务)";

    private final VmsFullBswjManagerService vmsFullBswjManagerService;

    private final VmsEnterpriseInfoRiskService vmsEnterpriseInfoRiskService;

    @XxlJob("vmsEntRiskJobHandler")
    public ReturnT<String> vmsEntRiskJobHandler() {
        long start = System.currentTimeMillis();
        XxlJobHelper.log("{}开始执行", LOG_MSG);

        // 分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        XxlJobHelper.log("{}分片参数，当前分片序号：{}, 总分片数：{}", LOG_MSG, shardIndex, shardTotal);

        // 获取接入数电的机构配置信息
        List<VmsFullBswjManager> sdDeptInfoList = vmsFullBswjManagerService.getAllSdDeptInfo();
        boolean success = true;
        if (CollectionUtil.isNotEmpty(sdDeptInfoList)) {
            for (int i = 0; i < sdDeptInfoList.size(); i++) {
                try {
                    if (i % shardTotal != shardIndex) {
                        XxlJobHelper.log("{}第{}次循环，未命中分片，不做处理", LOG_MSG, i + 1);
                        continue;
                    }
                    VmsFullBswjManager bswjManager = sdDeptInfoList.get(i);
                    XxlJobHelper.log("{}第{}次循环，机构编码：{}，纳税人名称：{}，纳税人识别号：{}",
                            LOG_MSG, i + 1, bswjManager.getWhjg(), bswjManager.getNsztName(), bswjManager.getNsrsbh());
                    // 获取企业纳税人风险信息
                    VmsEnterpriseInfoRiskDto dto = new VmsEnterpriseInfoRiskDto();
                    dto.setNsrsbh(bswjManager.getNsrsbh());
                    TokenUser tokenUser = new TokenUser();
                    tokenUser.setDeptCode(bswjManager.getWhjg());
                    tokenUser.setUserCode("定时任务");
                    tokenUser.setUserName("定时任务");
                    Result<Object> result = vmsEnterpriseInfoRiskService.getVmsEnterpriseInfoRiskByLq(dto, tokenUser);
                    if (ResponseEnum.SUCCESS_MSG.getCode().equals(result.getCode())) {
                        XxlJobHelper.log("{}获取成功", LOG_MSG);
                    } else {
                        XxlJobHelper.log("{}获取失败，错误信息：{}", LOG_MSG, result.getMessage());
                        success = false;
                    }
                } catch (Exception e) {
                    XxlJobHelper.log("{}获取失败，错误信息：{}", LOG_MSG, e.getMessage());
                    success = false;
                }
            }
        }

        long end = System.currentTimeMillis();
        XxlJobHelper.log("{}执行完成，用时：{}ms", LOG_MSG, end - start);
        if (success) {
            return ReturnT.SUCCESS;
        } else {
            XxlJobHelper.handleFail();
            return ReturnT.FAIL;
        }
    }
}
